/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Config.java
 *
 * Created on Jul 21, 2011, 2:02:37 PM
 */
package generalCode;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author Hoai An
 */
public class Config extends javax.swing.JFrame {

    /** Creates new form Config */
    public Config() {
        initComponents();
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setTitle("Config");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnContent = new javax.swing.JPanel();
        lbConfig = new javax.swing.JLabel();
        lbServerName = new javax.swing.JLabel();
        lbDatabaseName = new javax.swing.JLabel();
        lbPort = new javax.swing.JLabel();
        lbUserName = new javax.swing.JLabel();
        lbPassword = new javax.swing.JLabel();
        tfServerName = new javax.swing.JTextField();
        tfPort = new javax.swing.JTextField();
        tfDatabaseName = new javax.swing.JTextField();
        tfPassword = new javax.swing.JPasswordField();
        tfUserName = new javax.swing.JTextField();
        btConfig = new javax.swing.JButton();
        lbPlease = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lbConfig.setFont(new java.awt.Font("Tahoma", 1, 14));
        lbConfig.setForeground(new java.awt.Color(0, 0, 204));
        lbConfig.setText("Config Database");

        lbServerName.setText("Server Name");

        lbDatabaseName.setText("Database Name");

        lbPort.setText("Port");

        lbUserName.setText("User Name");

        lbPassword.setText("Password");

        tfServerName.setText("localhost");

        tfPort.setText("1433");
        tfPort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfPortActionPerformed(evt);
            }
        });

        tfDatabaseName.setText("Remedyscheduler");

        tfPassword.setText("qwerty");

        tfUserName.setText("sa");

        btConfig.setText("Config");
        btConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConfigActionPerformed(evt);
            }
        });

        lbPlease.setText("Please, config to database before login!");

        javax.swing.GroupLayout pnContentLayout = new javax.swing.GroupLayout(pnContent);
        pnContent.setLayout(pnContentLayout);
        pnContentLayout.setHorizontalGroup(
            pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnContentLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbServerName)
                    .addComponent(lbUserName)
                    .addComponent(lbPassword)
                    .addComponent(lbPort)
                    .addComponent(lbDatabaseName))
                .addGap(18, 18, 18)
                .addGroup(pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(tfServerName, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                    .addComponent(tfPort)
                    .addComponent(tfDatabaseName)
                    .addComponent(tfUserName)
                    .addComponent(tfPassword))
                .addContainerGap(38, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnContentLayout.createSequentialGroup()
                .addContainerGap(136, Short.MAX_VALUE)
                .addComponent(btConfig)
                .addGap(131, 131, 131))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnContentLayout.createSequentialGroup()
                .addContainerGap(108, Short.MAX_VALUE)
                .addComponent(lbConfig)
                .addGap(109, 109, 109))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnContentLayout.createSequentialGroup()
                .addContainerGap(73, Short.MAX_VALUE)
                .addComponent(lbPlease)
                .addGap(65, 65, 65))
        );
        pnContentLayout.setVerticalGroup(
            pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnContentLayout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(lbConfig)
                .addGap(11, 11, 11)
                .addComponent(lbPlease)
                .addGap(18, 18, 18)
                .addGroup(pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbServerName)
                    .addComponent(tfServerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbPort)
                    .addComponent(tfPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfDatabaseName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbDatabaseName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbUserName)
                    .addComponent(tfUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbPassword)
                    .addComponent(tfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btConfig)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnContent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnContent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tfPortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfPortActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfPortActionPerformed

    private void btConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConfigActionPerformed
        if (validateInput() && checkConnectToDB()) {
            JOptionPane.showMessageDialog(this, "Config to database is successfully!");
            this.dispose();
            new Login();
            
        }
    }//GEN-LAST:event_btConfigActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btConfig;
    private javax.swing.JLabel lbConfig;
    private javax.swing.JLabel lbDatabaseName;
    private javax.swing.JLabel lbPassword;
    private javax.swing.JLabel lbPlease;
    private javax.swing.JLabel lbPort;
    private javax.swing.JLabel lbServerName;
    private javax.swing.JLabel lbUserName;
    private javax.swing.JPanel pnContent;
    private javax.swing.JTextField tfDatabaseName;
    private javax.swing.JPasswordField tfPassword;
    private javax.swing.JTextField tfPort;
    private javax.swing.JTextField tfServerName;
    private javax.swing.JTextField tfUserName;
    // End of variables declaration//GEN-END:variables

    public boolean checkConnectToDB() {

        String serverName = this.tfServerName.getText();
        String databaseName = this.tfDatabaseName.getText();
        String port = this.tfPort.getText();
        String userName = this.tfUserName.getText();
        char[] password = this.tfPassword.getPassword();
        String pass = new String(password);
        Connectionsql connect = Connectionsql.newInstance();
        try {
            return connect.checkConnection(serverName, port, databaseName, userName, pass);
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "ERROR: " + ex.getMessage(), "Messages", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }


    private boolean validateInput() {
        if (this.tfServerName.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Enter server name");
            return false;
        }
        return true;
    }
}
